<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库记录ID" prop="recordId">
        <el-input
          v-model="queryParams.recordId"
          placeholder="请输入出库记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入物资ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库数量" prop="goodsNum">
        <el-input
          v-model="queryParams.goodsNum"
          placeholder="请输入出库数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="goodsUnit">
        <el-select v-model="queryParams.goodsUnit" placeholder="请选择单位" clearable>
          <el-option
            v-for="dict in dict.type.goods_unit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否需要归还" prop="returnFlag">
        <el-select v-model="queryParams.returnFlag" placeholder="请选择是否需要归还" clearable>
          <el-option
            v-for="dict in dict.type.yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归还状态" prop="returnStatus">
        <el-select v-model="queryParams.returnStatus" placeholder="请选择归还状态" clearable>
          <el-option
            v-for="dict in dict.type.return_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否入库" prop="enterFlag">
        <el-select v-model="queryParams.enterFlag" placeholder="请选择是否入库" clearable>
          <el-option
            v-for="dict in dict.type.yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归还数量" prop="returnNum">
        <el-input
          v-model="queryParams.returnNum"
          placeholder="请输入归还数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归还人" prop="returnUser">
        <el-input
          v-model="queryParams.returnUser"
          placeholder="请输入归还人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取人单位" prop="returnUserUnit">
        <el-input
          v-model="queryParams.returnUserUnit"
          placeholder="请输入领取人单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取人职务" prop="returnUserPosition">
        <el-input
          v-model="queryParams.returnUserPosition"
          placeholder="请输入领取人职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取人电话" prop="returnUserPhone">
        <el-input
          v-model="queryParams.returnUserPhone"
          placeholder="请输入领取人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归还时间" prop="returnTime">
        <el-date-picker clearable
          v-model="queryParams.returnTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择归还时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="归还备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入归还备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:detail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="出库记录ID" align="center" prop="recordId" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="物资ID" align="center" prop="goodsId" />
      <el-table-column label="物资名称" align="center" prop="goodsName" />
      <el-table-column label="物资图片" align="center" prop="goodsPic" />
      <el-table-column label="出库数量" align="center" prop="goodsNum" />
      <el-table-column label="单位" align="center" prop="goodsUnit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_unit" :value="scope.row.goodsUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="是否需要归还" align="center" prop="returnFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.returnFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="归还状态" align="center" prop="returnStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.return_status" :value="scope.row.returnStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否入库" align="center" prop="enterFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.enterFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="归还数量" align="center" prop="returnNum" />
      <el-table-column label="归还人" align="center" prop="returnUser" />
      <el-table-column label="领取人单位" align="center" prop="returnUserUnit" />
      <el-table-column label="领取人职务" align="center" prop="returnUserPosition" />
      <el-table-column label="领取人电话" align="center" prop="returnUserPhone" />
      <el-table-column label="归还时间" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归还备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物资出库明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库记录ID" prop="recordId">
          <el-input v-model="form.recordId" placeholder="请输入出库记录ID" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="物资ID" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入物资ID" />
        </el-form-item>
        <el-form-item label="物资名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入物资名称" />
        </el-form-item>
        <el-form-item label="物资图片" prop="goodsPic">
          <el-input v-model="form.goodsPic" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="出库数量" prop="goodsNum">
          <el-input v-model="form.goodsNum" placeholder="请输入出库数量" />
        </el-form-item>
        <el-form-item label="单位" prop="goodsUnit">
          <el-select v-model="form.goodsUnit" placeholder="请选择单位">
            <el-option
              v-for="dict in dict.type.goods_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否需要归还" prop="returnFlag">
          <el-select v-model="form.returnFlag" placeholder="请选择是否需要归还">
            <el-option
              v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归还状态" prop="returnStatus">
          <el-select v-model="form.returnStatus" placeholder="请选择归还状态">
            <el-option
              v-for="dict in dict.type.return_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否入库" prop="enterFlag">
          <el-select v-model="form.enterFlag" placeholder="请选择是否入库">
            <el-option
              v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归还数量" prop="returnNum">
          <el-input v-model="form.returnNum" placeholder="请输入归还数量" />
        </el-form-item>
        <el-form-item label="归还人" prop="returnUser">
          <el-input v-model="form.returnUser" placeholder="请输入归还人" />
        </el-form-item>
        <el-form-item label="领取人单位" prop="returnUserUnit">
          <el-input v-model="form.returnUserUnit" placeholder="请输入领取人单位" />
        </el-form-item>
        <el-form-item label="领取人职务" prop="returnUserPosition">
          <el-input v-model="form.returnUserPosition" placeholder="请输入领取人职务" />
        </el-form-item>
        <el-form-item label="领取人电话" prop="returnUserPhone">
          <el-input v-model="form.returnUserPhone" placeholder="请输入领取人电话" />
        </el-form-item>
        <el-form-item label="归还时间" prop="returnTime">
          <el-date-picker clearable
            v-model="form.returnTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择归还时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="归还备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入归还备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/goods/detail";

export default {
  name: "Detail",
  dicts: ['return_status', 'goods_unit', 'yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资出库明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordId: null,
        warehouseId: null,
        warehouseName: null,
        goodsId: null,
        goodsName: null,
        goodsPic: null,
        goodsNum: null,
        goodsUnit: null,
        returnFlag: null,
        returnStatus: null,
        enterFlag: null,
        returnNum: null,
        returnUser: null,
        returnUserUnit: null,
        returnUserPosition: null,
        returnUserPhone: null,
        returnTime: null,
        remarks: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recordId: [
          { required: true, message: "出库记录ID不能为空", trigger: "blur" }
        ],
        warehouseId: [
          { required: true, message: "仓库ID不能为空", trigger: "blur" }
        ],
        warehouseName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        goodsId: [
          { required: true, message: "物资ID不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "物资名称不能为空", trigger: "blur" }
        ],
        goodsNum: [
          { required: true, message: "出库数量不能为空", trigger: "blur" }
        ],
        goodsUnit: [
          { required: true, message: "单位不能为空", trigger: "change" }
        ],
        returnFlag: [
          { required: true, message: "是否需要归还不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物资出库明细列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recordId: null,
        warehouseId: null,
        warehouseName: null,
        goodsId: null,
        goodsName: null,
        goodsPic: null,
        goodsNum: null,
        goodsUnit: null,
        returnFlag: null,
        returnStatus: null,
        enterFlag: null,
        returnNum: null,
        returnUser: null,
        returnUserUnit: null,
        returnUserPosition: null,
        returnUserPhone: null,
        returnTime: null,
        remarks: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物资出库明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物资出库明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物资出库明细编号为"' + ids + '"的数据项？').then(function() {
        return delDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
